package com.rlz.framemm.ui

import com.rlz.annotation.NeedLogin
import com.rlz.base.base.BaseActivity
import com.rlz.base.cache.CacheManager
import com.rlz.framemm.R
import com.rlz.framemm.bean.BannerBean
import com.rlz.framemm.databinding.ActivityTestBinding
import com.rlz.framemm.model.TestModel
import com.rlz.framemm.viewmodel.TestViewModel
import java.util.ArrayList

/**
 * Created by RLZ
 * on 2021/9/16
 *
 */
@NeedLogin
class TestActivity : BaseActivity<TestModel, TestViewModel, ActivityTestBinding>() {

    private var mUrls = ArrayList<String>()
    private var mTitles = ArrayList<String>()

    override fun layoutId(): Int {
        return R.layout.activity_test
    }

    override fun logic() {
        mViewModel.bannerData.observe(this, { result ->
            val response = result.getOrNull()
            toast("执行click$response")
            if (response == null || !response.isSuccess()) return@observe

            if (response.getData().isNullOrEmpty()) return@observe

            updateData(response.getData())

        })
        mBinding.bt.text = CacheManager.Companion.manager.getUserInfo()?.nickName
    }

    private fun updateData(data: List<BannerBean>?) {

        if (data == null || data.isEmpty()) return

        mUrls = ArrayList<String>()
        mTitles = ArrayList<String>()
        for (i in data.indices) {
            mUrls.add(data[i].imagePath)
            mTitles.add(data[i].title)
        }
        mBinding.bannerView.setBannerTitles(mTitles)
        mBinding.bannerView.setImages(mUrls)
        mBinding.bannerView.start()
    }

    override fun click() {
        super.click()
        mBinding.bt.setOnClickListener {
            toast("执行click")

        }
    }
}